package com.csii.activiti.server.core.service.impl;

import com.csii.activiti.server.core.entity.ActFlowServiceNode;
import com.csii.activiti.server.core.listener.base.BaseServiceNodeExecutor;
import com.csii.activiti.server.core.service.FlowServerNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FlowServerNodeServiceImpl implements FlowServerNodeService {

    private List<ActFlowServiceNode> serviceNodes;

    @Autowired(required = false)
    private void initServiceNodes(List<BaseServiceNodeExecutor> serviceNodes){
        this.serviceNodes = serviceNodes.stream().filter(item -> item.isShowList()).map(item -> {
            //String beanName = item.getClass().getSimpleName();
            ActFlowServiceNode serviceNode = new ActFlowServiceNode();
            serviceNode.setDescription(item.serviceNodeDescription());
            String beanName = item.beanName();
            if (beanName == null) {
                beanName = item.getClass().getSimpleName();
                String one = String.valueOf(beanName.charAt(0)).toLowerCase();
                beanName = one + beanName.substring(1);
            }
            serviceNode.setExpression("${" + beanName + "}");
            return serviceNode;
        }).collect(Collectors.toList());
    }


    @Override
    public List<ActFlowServiceNode> queryServiceNodes() {
        return serviceNodes;
    }
}
